import App from './App'

import uView from '@/uni_modules/uview-ui'
import bottomTab from './static/component/bottomTab.vue'
Vue.component('bottom-tab', bottomTab)
Vue.use(uView)
import MyComponent from '@/pages/login/password.vue'
Vue.component('pass-tab', MyComponent)

const url = 'https://guanli.yingtaihuanbao.tech/prod-api/';
const ajax = function(methods, ajaxurl, object_) {
	const token = uni.getStorageSync('key');
	let ajaxobj = object_ ? object_ : {};
	return new Promise((resolve, reject) => {
		if (ajaxobj.loding === false) {
			delete ajaxobj.loding
		} else {
			uni.showLoading({
				title: '加载中~'
			})
		};
		let header = {
				'Content-Language': 'zh_CN'
			};
			if (token) {
				header[ 'Authorization' ] = 'Bearer ' + token
			}
		uni.request({
			data: ajaxobj,
			method: methods.toUpperCase(),
			url: url + ajaxurl,
			dataType: "json",
			header,
			success(res) {
				uni.hideLoading()
				if (res.data.code * 1 == 200) {
					resolve(res.data)
				} else {
					msg(res.data.msg)
					resolve(res.data)
				}
			},
			fail(res) {
				uni.hideLoading()
				uni.getNetworkType({
					success: function(res) {
						if (res.networkType == 'none') {
							uni.showToast({
								title: '网络开小差了···',
								icon: "none"
							})
						}
					}
				});
			}
		})
	})
}
let tokenKey = ()=>{
	let token = uni.getStorageSync('key')
	if(token){
		ajax('get', 'recovery/user/profile').then((res1) => {
			var userId = res1.data.user.userId
			if (res1.code == 200) {
				if (res1.data.user.recyclerType == 1) {
					msg('登录成功')
					console.log(userId);
					uni.redirectTo ({
						url: '/pages/index/index?userId=' + userId
					})
				} else {
					console.log(123);
					msg('登录成功')
					uni.redirectTo({
						url: '/pages/index/scanCode'
					})
				}

			}
		})
	}
}
const msg = (title, duration = 1500, mask = false, icon = 'none') => {
	//统一提示方便全局修改
	if (Boolean(title) === false) {
		return;
	}
	uni.showToast({
		title,
		duration,
		mask,
		icon,
	});
}
const prePage = () => {
	let pages = getCurrentPages();
	let prePage = pages[pages.length - 2];
	// #ifdef H5
	return prePage;
	// #endif
	return prePage.$vm;
}
Vue.prototype.$ajax = ajax;
Vue.prototype.$url = url;
Vue.prototype.$tokenKey = tokenKey;
Vue.prototype.$api = {
	msg,
	prePage
};

// #ifndef VUE3
import Vue from 'vue'
Vue.config.productionTip = false
App.mpType = 'app'
const app = new Vue({
	...App
})
app.$mount()
// #endif

// #ifdef VUE3
import {
	createSSRApp
} from 'vue'
export function createApp() {
	const app = createSSRApp(App)
	return {
		app
	}
}
// #endif
